﻿(function ($) {
    //分组

    $.fn.groupByTable = function (options) {

        var defaults = {
            data:null,
            i:0,
            cols: [],
         callback:null
        };
        var options = $.extend(defaults, options);
        //  console.log(options)
   return $(this).each(function () {
     
       var row;
       console.log(!$(this).data("groupTableRow") ||  $(this).data("groupTableRow") == 0);
       if (options.i == 0) {
           $(this).data("groupTableRow", 0);
           row = 0;
           console.log($(this).data("groupTableRow"));

       } else {

           row = $(this).data("groupTableRow");
       }

       if (row == options.i) {
           
         for (var j = options.i; j < options.data.length; j++) {
             if (j < options.data.length - 1) {
                        var c = "";
                        $.each(options.cols, function (ii, obj) {

                            if (ii == 0)
                                c += " options.data[j + 1]['" + obj.colName + "'] != options.data[j]['" + obj.colName + "']  ";
                            else
                                c += " || " + " options.data[j + 1]['" + obj.colName + "'] != options.data[j]['" + obj.colName + "']  ";
                        });
                 console.log(c)
                        if (eval(c)) {
                           
                            $.each(options.cols, function (ii, obj) {

                                if (obj.show) {
                                    
                                    if (options.callback)
                                        options.callback(obj, options.i, j);
                                }

                            });
                            row = j + 1;
                            break;
                        }
                    }
                    else {
                 $.each(options.cols, function (ii, obj) {
                            if (obj.show) {
                                if (options.callback)
                                    options.callback(obj, options.i, j);
                            }

                        });
                 row = j + 1;
                
                        break;
                    }

                }
            }
     $(this).data("groupTableRow", row);

      });

    };

})(window.jQuery)